package com.yisu.auction.manager.core.service.impl;

import com.google.common.collect.Lists;
import com.yisu.auction.ewf.entity.GoodsInfo;
import com.yisu.auction.ewf.entity.GoodsType;
import com.yisu.auction.manager.core.mapper.GoodsInfoMapper;
import com.yisu.auction.manager.core.mapper.GoodsTypeMapper;
import com.yisu.auction.manager.core.service.IDataAnalysisService;
import com.yisu.auction.manager.domain.vo.dataAnalysis.AnalysisGoodsVo;
import com.yisu.auction.manager.domain.vo.dataAnalysis.AnalysisImageVo;
import org.apache.calcite.linq4j.Linq4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * author: 赵杰
 * Business Description：
 * createTime：2017/9/7
 */
@Service("IDataAnalysisService")
public class DataAnalysisServiceImpl extends BaseService implements IDataAnalysisService {
    @Resource
    private GoodsInfoMapper goodsInfoMapper;
    @Resource
    private GoodsTypeMapper goodsTypeMapper;

    /**
     * author: 赵杰
     * Business Description：添加商品信息
     *
     * @param analysisGoodsVos 商品主解析数据
     * @return
     */
    @Override
    public boolean addGoodsInfo(List<AnalysisGoodsVo> analysisGoodsVos) {
        if (!CollectionUtils.isEmpty(analysisGoodsVos)) {
            for (AnalysisGoodsVo goodsItem : analysisGoodsVos) {
                //商品类目
                long goosTypeId = getGoodsTypeIdByTypeName(goodsItem.getCategoryName());
                GoodsInfo insertGoodsInfo = new GoodsInfo();
                insertGoodsInfo.setCreateDate(new Date());
                insertGoodsInfo.setIsValid(true);
                insertGoodsInfo.setGoodsTypeId(goosTypeId);
                insertGoodsInfo.setName(goodsItem.getGoodsName());
                insertGoodsInfo.setImages(getImageUrl(goodsItem.getGoodsMainImages()));
                insertGoodsInfo.setSalesNo(goodsItem.getSalesNo());
                insertGoodsInfo.setStock(100000000);
                insertGoodsInfo.setSalesType(1);
                insertGoodsInfo.setTerm(0);
                insertGoodsInfo.setStatus(1);
                insertGoodsInfo.setWaterLevel(new BigDecimal(goodsItem.getWaterLevel()));
                insertGoodsInfo.setIsRechargeableCard(1);
                if (goodsItem.getIsRechargeCard().contains("否")) {
                    insertGoodsInfo.setIsRechargeableCard(0);
                }
                insertGoodsInfo.setIsTen((byte) 1);
                if (goodsItem.getIsTen().contains("否")) {
                    insertGoodsInfo.setIsTen((byte) 0);
                }
                insertGoodsInfo.setDetail(getImageUrl(goodsItem.getGoodsMainImages()));
                String detailInfo = "";
                if (!StringUtils.isEmpty(insertGoodsInfo.getDetail())) {
                    List<String> detailInfoArray = Linq4j.asEnumerable(insertGoodsInfo.getDetail().split(",")).toList();
                    for (String item : detailInfoArray) {
                        detailInfo += "<p><img src=\"" + item + "\" _src=\"" + item + "\"/></p>";
                    }
                }
                insertGoodsInfo.setDetail(detailInfo);
                insertGoodsInfo.setSort(goodsInfoMapper.getMaxSort() + 1);
                goodsInfoMapper.insertSelective(insertGoodsInfo);
            }
        }
        return true;
    }


    /**
     * author: 赵杰
     * Business Description：通过类型名称获取商品类型ID
     *
     * @param typeName
     * @return
     */
    protected long getGoodsTypeIdByTypeName(String typeName) {
        GoodsType goodsType = goodsTypeMapper.selectByTypeName(typeName);
        if (goodsType == null) {
            GoodsType insertModel = new GoodsType();
            insertModel.setCreateDate(new Date());
            insertModel.setDescrib(typeName);
            insertModel.setName(typeName);
            insertModel.setIsValid(true);
            insertModel.setSort(1000);
            goodsTypeMapper.insertSelective(insertModel);
            goodsType = goodsTypeMapper.selectByTypeName(typeName);
            return goodsType.getId();
        }
        return goodsType.getId();
    }


    /**
     * author: 赵杰
     * Business Description：获取商品主图信息
     *
     * @param images
     * @return
     */
    public String getImageUrl(List<AnalysisImageVo> images) {
        if (!CollectionUtils.isEmpty(images)) {
            List<String> ImagesArray = Lists.newArrayList();
            for (AnalysisImageVo analysisImageVo : images) {
                if (!org.apache.commons.lang3.StringUtils.isEmpty(analysisImageVo.getImagePath())) {
                    String path = uploadFileForLocation(new File(analysisImageVo.getImagePath()));
                    if (!org.apache.commons.lang3.StringUtils.isEmpty(path))
                        ImagesArray.add(path.trim());
                }
            }
            return org.apache.commons.lang3.StringUtils.join(ImagesArray, ",");
        } else {
            return "";
        }
    }


}
